import json
import sys
import pandas as pd
import time

from absl import app, flags
from python.coin2.api_client.Binance import BinancePrivateClient


def batch_update(binance: BinancePrivateClient):
  res = []
  csv_path = flags.FLAGS.batch_update_path
  df = pd.read_csv(csv_path)
  #print(df.to_string())
  for _, row in df.iterrows():
    api_key = row['apikey']
    email = row['email']
    ip_list = row['ip']
    ## enable ip bind
    #info = binance.enable_bind_ip(api_key, email)
    #print(json.dumps(info, indent=2))
    ## add bind ip
    #info = binance.add_bind_ip_list(api_key, email, ip_list)
    #print(json.dumps(info, indent=2))
    ## delete bind ip
    ## info = binance.del_bind_ip_list(api_key, email, ip_list)
    ## print(json.dumps(info, indent=2))
    ## get bind ip list
    info = binance.get_bind_ip_list(api_key, email)
    print(json.dumps(info, indent=2))
    res.append(info)

    time.sleep(1)
    break

  #print(res)
  with open(csv_path.replace(".csv", ".json"), 'w') as f:
    f.write(json.dumps(res, indent=2))


def single_update(binance: BinancePrivateClient):
  api_key = flags.FLAGS.api_key
  email = flags.FLAGS.email
  ip_list = flags.FLAGS.ip_list

  #info = binance.enable_bind_ip(api_key, email)
  #print(json.dumps(info, indent=2))
  #info = binance.add_bind_ip_list(api_key, email, ip_list)
  #print(json.dumps(info, indent=2))
  #info = binance.del_bind_ip_list(api_key, email, ip_list)
  #print(json.dumps(info, indent=2))
  info = binance.get_bind_ip_list(api_key, email)
  print(json.dumps(info, indent=2))
  #print(json.dumps(resp, indent=2))


def main(_):
  binance = BinancePrivateClient(flags.FLAGS.key_path, "Spot.Binance.v1")
  if flags.FLAGS.batch_update_path is not None:
    batch_update(binance)
  else:
    single_update(binance)


if __name__ == '__main__':
  flags.DEFINE_string('batch_update_path', None, '')
  flags.DEFINE_string('key_path', None, '')
  flags.DEFINE_string('api_key', None, '')
  flags.DEFINE_string('email', None, '')
  flags.DEFINE_string('ip_list', None, '')

  app.run(main)
